package flextv.main.command {

import com.adobe.cairngorm.commands.ICommand;
import com.adobe.cairngorm.control.CairngormEvent;

import flextv.core.logic.Logger;
import flextv.core.vo.Playlist;
import flextv.core.vo.StoredItem;
import flextv.main.logic.MainServices;
import flextv.search.event.AddSearchItemEvent;
import flextv.search.logic.SearchModel;
import flextv.search.vo.SearchItem;

import mx.collections.ArrayCollection;
import mx.logging.ILogger;

public class AddSearchItemCommand implements ICommand {
	
	private static const LOG: ILogger = Logger.getLog(AddSearchItemCommand);
	
	public function AddSearchItemCommand() {
	}

	public function execute(_event: CairngormEvent): void {
		const event: AddSearchItemEvent = _event as AddSearchItemEvent;
		LOG.info("Executing [event=" + event + "].");
		
		const searchItem: SearchItem = event.searchItem;
		const targetPlaylist: Playlist = event.targetPlaylist;
		
		
		// remove search item from list
		const searchItems: ArrayCollection = SearchModel.instance.searchItems;
		const searchItemIndex: int = searchItems.getItemIndex(searchItem);
		searchItems.removeItemAt(searchItemIndex);
		
		SearchModel.instance.currentSearchItem = null;
		
		const newStoredItem: StoredItem = searchItem.toStoredItem();
		// mapping playlist ID not necessary ... newStoredItem.playlistId = targetPlaylist.id;
		
		
		// delegate to AddItemDelegate
		MainServices.instance.addItemService.downloadAddItem(newStoredItem, targetPlaylist, searchItem.thumbnailUrl);
	}
	
}
}